<!--
 * @Author: your name
 * @Date: 2021-08-06 15:15:05
 * @LastEditTime: 2021-08-06 16:26:28
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \vuestudy\vue\19-插槽\具名插槽.html
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <link rel="stylesheet" href="../15-组件/css/common.css" />
</head>
<style>
    .contain {
        width: 400px;
        height: 400px;
        border: 1px solid #efa210;
    }

    .contain div {
        width: 100%;
        line-height: 40px;
        background-color: aqua;
        margin: auto;
        text-align: center;
        border: 1px solid #a655aa;
        margin-top: 20px;
    }

    .contain ul {
        width: 50%;
        margin: auto;
    }

    .contain li {
        display: flex;
        justify-content: space-between;
        margin: 2px;
        padding: 0 20px;
    }

    button {
        width: 40px;
        background-color: aliceblue;
        border: none;
        border-bottom: 1px solid #a655aa;
    }
</style>

<body>
    <div id="app">
        <mylayout :stus="stus" title="学生管理系统" @delclick="delstu">
            <!-- scope对象里面保存就是slot标签里面绑定的属性 -->
            <template v-slot:default="scope">
                <!-- <button @click="delstu(scope.index)">删除</button>
                    {{scope}} -->
                <a href="javascript:;" @click="delstu(scope.index)">删除</a>

                <!-- 到此 组件里面的删除标签就活了 可以随便换元素 -->

            </template>
        </mylayout>
    </div>
    <script>
        // 作用域插槽：在slot标签里面，可以绑定属性，渲染后 绑定的属性就在标签的v-slot：defalut对象中
        Vue.component("mylayout", {
            props: ["stus", "title"],
            template: `
            <div class="contain">
                <div>{{title}}</div>
                <div>
                    <ul>
                        <li v-for="(item,i) in stus">{{item.name}}
                            <!--
                            <button @click="del(i)">删除</button>-->
                            <!--在插槽上面绑定属性 index并且值为i-->
                            <slot v-bind:index="i"></slot>
                        </li>
                    </ul>
                </div>
            </div>
            `,
            methods: {
                del(i) {
                    this.$emit("delclick", i)
                }
            },
        });
        new Vue({
            el: "#app",
            data() {
                return {
                    stus: [{
                            id: 1,
                            name: 11,
                        },
                        {
                            id: 2,
                            name: 22,
                        },
                        {
                            id: 3,
                            name: 33,
                        },
                        {
                            id: 4,
                            name: 44,
                        },
                        {
                            id: 5,
                            name: 55,
                        },
                    ],
                };
            },
            methods: {
                delstu(i) {
                    this.stus.splice(i, 1)
                }
            },
        });
    </script>
</body>

</html>